﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using V5_Model;
using V5_Common;
using V5_BLL;

namespace V5_EShop.Admin {
    public partial class Admin_Announce : BasicPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                Action = InputHelper.SetString(Request["Action"]);
                switch (Action.Trim().ToLower()) {
                    case "edit":
                        this.MultiViewList.ActiveViewIndex = 1;
                        break;
                    default:
                        Bind_gvList();
                        break;
                }
            }
        }

        private void Bind_gvList() {
            this.MultiViewList.ActiveViewIndex = 0;
            string strWhere = "";
            int count = 0;
            DataSet ds = new BLLAnnounce().GetList(Pager.PageSize, Pager.CurrentPageIndex, strWhere, ref count);
            Pager.RecordCount = count;
            this.gvList.DataSource = ds;
            this.gvList.DataBind();
        }

        protected void Pager_PageChanged(object sender, EventArgs e) {
            Bind_gvList();
        }

        protected void gvList_RowCommand(object sender, GridViewCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            int id = InputHelper.SetInt(e.CommandArgument);
            switch (eName.ToLower().Trim()) {
                case "go":
                    Bind_DataEdit(id);
                    break;
                case "del":
                    Bind_Delete(id);
                    break;
                default:
                    break;
            }
        }

        private void Bind_DataEdit(int id) {
            this.MultiViewList.ActiveViewIndex = 1;

            ModelAnnounce model = new BLLAnnounce().GetModel(id);
            if (model != null) {
                this.txtID.Value = model.ID.ToString();
                this.txtTitle.Text = model.AnnounceTitle;
                this.txtContent.Text = model.AnnounceContent;
                this.ddlIsShow.SelectedValue = model.IsShow.ToString();

                this.lblTitle.Text = "修改公告";
                this.btnSave.Text = "修改保存";
            }
        }

        private void Bind_Delete(int id) {
            new BLLAnnounce().Delete(id);
            this.Bind_gvList();
        }

        protected void btnSave_Click(object sender, EventArgs e) {
            int ID = InputHelper.SetInt(this.txtID.Value);
            string AnnounceTitle = this.txtTitle.Text;
            string AnnounceContent = this.txtContent.Text;
            DateTime AddDateTime = DateTime.Now;
            int IsShow = InputHelper.SetInt(this.ddlIsShow.SelectedValue);
            BLLAnnounce bll = new BLLAnnounce();
            ModelAnnounce model = new ModelAnnounce();
            model.AnnounceTitle = AnnounceTitle;
            model.AnnounceContent = AnnounceContent;
            model.AddDateTime = AddDateTime;
            model.IsShow = IsShow;

            if (ID == 0) {
                model.ID = bll.GetMaxId();
                bll.Add(model);
            }
            else if (ID > 0) {
                model.ID = ID;
                bll.Update(model);
            }

            this.Bind_gvList();
        }
    }
}
